package com.gitee.candy.encry;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.util.Base64;

public class EncryptBASE64Utils {

    /**
     * BASE64加密 sun.misc
     */
    public static String encryptSun(String key) throws Exception {
        return (new BASE64Encoder()).encodeBuffer(key.getBytes());
    }

    /**
     * BASE64解密 sun.misc
     *
     * @throws Exception
     */
    public static String decryptSun(String key) throws Exception {
        return new String((new BASE64Decoder()).decodeBuffer(key));
    }


    /**
     * BASE64加密
     */
    public static String encrypt(String key) throws Exception {
        return Base64.getEncoder().encodeToString(key.getBytes());
    }

    /**
     * BASE64解密
     *
     * @throws Exception
     */
    public static String decrypt(String key) throws Exception {
        return new String(Base64.getDecoder().decode(key));
    }

    public static byte[] decode(String data) throws Exception {
        return Base64.getDecoder().decode(data);
    }

    public static byte[] decode(byte[] data) throws Exception {
        return Base64.getDecoder().decode(data);
    }

    public static String encode(byte[] encrypted) throws Exception {
        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static byte[] encodeByte(byte[] encrypted) throws Exception {
        return Base64.getEncoder().encode(encrypted);
    }

    public static void main(String[] args) throws Exception {
        String str = "宝剑分从磨砺出，梅花香自苦寒来";

        String s = encryptSun(str);
        System.out.println(s);

        String s1 = decryptSun(s);
        System.out.println(s1);

        String data = encrypt(s1);
        System.out.println(data);

        String de = decrypt(data);
        System.out.println(de);
    }
}
